import logging
from functools import partial

from PyQt5.QtWidgets import QTableWidgetItem, QHeaderView

from .pagevac_base import SubPageWithTableVac
# from ioslave.slave import get_pin_value, get_pin_forced_value

LIST_PLC_UI_DATA = []  # PLC.ui file data

class UIConfPageVac(SubPageWithTableVac):
    def __init__(self, parent=None):
        self.table_group_num = 2  # 定义表格每行并行显示几组数据
        super().__init__(parent)

        self.table_data_update(self.tableWidget, LIST_PLC_UI_DATA)

    def table_data_update(self, table_wx, list_plc_ui):
        self.table_data_clear(table_wx)  # 清空数据

        len_tuple_ui_info = len(list_plc_ui[0])
        current_table_group_index = 0
        row_index = 0
        for tuple_ui_info in list_plc_ui:
            if current_table_group_index == 0:
                table_wx.insertRow(row_index)  # 动态添加行

            for column_index in range(len_tuple_ui_info):
                # 动态写入单元格数据
                table_wx.setItem(row_index, 
                        column_index + current_table_group_index * len_tuple_ui_info, 
                        QTableWidgetItem(tuple_ui_info[column_index]))

            # 改写当前编辑组数
            if current_table_group_index < self.table_group_num-1:
                current_table_group_index += 1
            else:
                row_index += 1
                current_table_group_index = 0

    # @override
    def table_showing_style(self, table_wx):
        header = table_wx.horizontalHeader()
        for column_index in range(len(self.subpage_mapping)):
            if column_index % self.table_group_num == self.table_group_num-1:
                header.setSectionResizeMode(column_index, QHeaderView.ResizeToContents)  # 额，为啥列宽不能适配了??
            else:
                header.setSectionResizeMode(column_index, QHeaderView.Stretch)
